<?php

/**
 * 会员中心
 */
namespace User\Controller;
use Common\Controller\MemberbaseController;
class CenterController extends MemberbaseController {
	
	protected $users_model;
	function _initialize(){
		parent::_initialize();
		$this->users_model=D("Common/Users");
	}
    //会员中心
	public function index() {
		$userid=sp_get_current_userid();
		$user=$this->users_model->where(array("id"=>$userid))->find();
		$this->assign($user);
    	$this->display(':center');
    }
	
	public function add_article() {
      $userid=sp_get_current_userid();
	  $this->assign('userid',$userid);
	  $this->display(':add_article');
    }
	
	public function user_article()
	{
		$userid=sp_get_current_userid();
		$posts_model=M("Posts");
		//$term_relationships_model=M("TermRelationships");
		$join = "".C('DB_PREFIX').'term_relationships as r on r.object_id =p.id';
		$article=$posts_model->alias("p")->join($join)->where(array("post_author"=>$userid,"status"=>1))->select();
		
		//print_r($article);exit;
		$this->assign('article',$article);
		$this->display(':user_article');
	}
	
	public function edit_article()
	{
	//	
	   $id=  intval(I("get.id"));
	   //$article=sp_sql_post($id,'');
	   $posts_model=M("Posts");
	   $article=$posts_model->where(array("id"=>$id))->find();
	  // print($article['smeta']);
	 // $smeta=json_decode($article['smeta'], true); print($smeta);
		//print_r($article[smeta]);
		//$post=$article[0];
		//$term_relationship = M('TermRelationships')->where(array("object_id"=>$id,"status"=>1))->getField("term_id",true);
		//$this->_getTermTree($term_relationship);
		//$terms=$this->terms_model->select();
		//$post=$this->posts_model->where("id=$id")->find();
		//$this->assign("post",$post);
		$this->assign("smeta",json_decode($article['smeta'],true));
		//$this->assign("terms",$terms);
		$this->assign("id",$id);
	   $this->assign("article",$article);
	   $this->display(':edit_article');
	}
	
	public function do_edit_article() {
      
		$article=$_POST['post'];
		$article['post_author']=sp_get_current_userid();
		$article['recommended']=0;
		$article['istop']=0;
		$article['post_status']=0;
		
	//	print_r($article);exit;
        $temp = explode('/',$_POST['smeta'][thumb]); 
		$smeta['thumb']=$temp[count($temp)-2]."/".$temp[count($temp)-1];
		$article['smeta']=json_encode($smeta);
		
		$posts_model=M("Posts");
		$term_relationships_model=M("TermRelationships");
		$article['post_content']=htmlspecialchars_decode($article['post_content']);
		//print_r($article);exit;
	   $result=$posts_model->where('id='.$article[id])->save($article);
	    if ($result) {
			
				//$term_relationships_model->add(array("term_id"=>2,"object_id"=>$result));
			
				$this->success("添加成功！");
		} else {
				$this->error("添加失败！");
		}
	  
	   
    }
	
	public function delect_article()
	{
		$tid = intval(I("get.id"));
    	$term_relationships_model=M("TermRelationships");
		
			//$tid = intval(I("get.tid"));
		//	print_r($tid );exit;
			$data['status']=0;
			if ($term_relationships_model->where("tid=$tid")->save($data)) {
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		
	}
	
	public function do_add_article() {
      
		$article=$_POST['post'];
		$article['post_author']=sp_get_current_userid();
		$article['recommended']=0;
		$article['istop']=0;
		$article['post_status']=0;
		
		//print_r($article);exit;
        $temp = explode('/',$_POST['smeta'][thumb]); 
		$smeta['thumb']=$temp[count($temp)-2]."/".$temp[count($temp)-1];
		$article['smeta']=json_encode($smeta);
		
		$posts_model=M("Posts");
		$term_relationships_model=M("TermRelationships");;
		$article['post_content']=htmlspecialchars_decode($article['post_content']);
		//print_r($article);exit;
	    $result=$posts_model->add($article);
	    if ($result) {
			
				$term_relationships_model->add(array("term_id"=>2,"object_id"=>$result));
			
				$this->success("添加成功！");
		} else {
				$this->error("添加失败！");
		}
	  
	   
    }
	
	
}
